package com.liuf.di.controller;

import com.liuf.di.api.ServiceException;
import com.liuf.di.common.CommonResponse;
import com.liuf.di.common.ResponseUtil;
import com.liuf.di.configuration.kettle.KettleInitFactory;
import com.liuf.di.service.IJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.repository.Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: spring-cloud-liuf-master
 * @description: 任务管理
 * @author: Mr.Liu
 * @create: 2018-09-19 17:35
 * @码云： https://gitee.com/www.jiandan.com
 **/
@Api(value = "Job-manager",description = "任务管理API")
@RestController
@RequestMapping(value = "/api/job")
public class JobController {

    private static final Logger logger = LoggerFactory.getLogger(JobController.class);

    private Repository repository;

    @Autowired
    private KettleInitFactory kettleInitFactory;

    Map<String,Object> param = null;

    @Autowired
    private IJobService jobServiceImpl;

    private JobMeta jobMeta = null;

    @ApiOperation(value = "获取任务列表")
    @GetMapping(value = "/findJob")
    public CommonResponse findJob(@ApiParam(value = "分页参数-页数")@RequestParam(name = "pageNum", defaultValue = "0") int pageNum,
                                  @ApiParam(value = "分页参数-条数")@RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                  @ApiParam(value = "任务名称") @RequestParam(required = false) String name,
                                  @ApiParam(value = "任务详情") @RequestParam(required = false) String description){
        param = new HashMap<>();
        param.put("pageNum",pageNum);
        param.put("pageSize",pageSize);
        param.put("name",name);
        param.put("description",description);
        try {
            return ResponseUtil.generateResponse(jobServiceImpl.findJob(param));
        } catch (ServiceException e) {
            e.printStackTrace();
            return ResponseUtil.generateResponse(false);
        }
    }

    @ApiOperation(value = "获取单一任务")
    @GetMapping(value = "/getJob/{id}")
    public CommonResponse findJob(@ApiParam(value = "任务主键")@PathVariable long id){

        try {
            return ResponseUtil.generateResponse(jobServiceImpl.getJob(id));
        } catch (ServiceException e) {
            e.printStackTrace();
            return ResponseUtil.generateResponse(false);
        }
    }

    @ApiOperation(value = "删除任务[非级连删除]")
    @DeleteMapping(value = "/delete/{id}")
    public CommonResponse delete(@PathVariable long id){
        try {
            jobServiceImpl.delJob(id);
            return ResponseUtil.generateResponse(true);
        } catch (ServiceException e) {
            e.printStackTrace();
            return ResponseUtil.generateResponse(false);
        }

    }

    /**
     * 执行任务
     * @param id_directory JOB表 ID_DIRECTORY字段（非常重要切记）
     * @param name  任务名称
     * @return
     */
    @ApiOperation(value = "[JOB]手动执行任务")
    @PostMapping(value = "/execution/{id_directory}/{name}")
    public CommonResponse jobExecution(@ApiParam(value = "作业路径ID")@PathVariable long id_directory,@ApiParam(value = "作业名称")@PathVariable String name){
        jobMeta = new JobMeta();
        try {
            return ResponseUtil.generateResponse(jobServiceImpl.jobExecution(name,id_directory));
        } catch (ServiceException e) {
            e.printStackTrace();
            logger.error("任务执行失败"+e.getMessage());
            return null;
        }
    }

}
